package com.hw.one.collect.dust.redis;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;
import java.util.concurrent.TimeUnit;


@Repository
public class BaseRedis {
	
	@Resource
	private RedisTemplate<String,String> stringRedisTemplate;
	@Resource
	private RedisTemplate<Serializable, Serializable> redisTemplate;
	public void insert(String key,String value){

		stringRedisTemplate.opsForValue().set(key, value);
	}

	public void insertWithTime(String key,String value, int timeout){

		stringRedisTemplate.opsForValue().set(key, value, timeout, TimeUnit.SECONDS);
	}

	public void insertSer(String key,byte[] value){
		redisTemplate.opsForValue().set(key, value);
	}


	public void update(String key,String value){
		stringRedisTemplate.opsForValue().set(key, value);
	}

	public void addList(String key, String value) {
		stringRedisTemplate.opsForList().leftPush(key, value);
	}

	public void deleteList(String key, long count, Object value) {
		stringRedisTemplate.opsForList().remove(key,count,value);
	}

	public void deleteSerList(String key, long count, Object value) {
		redisTemplate.opsForList().remove(key,count,value);
	}

	public void addObjList(String key, Object obj) {
		redisTemplate.opsForList().leftPush(key, (Serializable) obj);
	}

	public List<String> getList(String key) {
		return stringRedisTemplate.opsForList().range(key, 0, -1);
	}
	public List<Serializable> getObjList(String key) {
		return redisTemplate.opsForList().range(key, 0, -1);
	}

	public void delete(String key){
		stringRedisTemplate.delete(key);
	}
	public void deleteSer(String key){
		redisTemplate.delete(key);
	}

	public String get(String key){
		return stringRedisTemplate.opsForValue().get(key);
	}
	public Serializable getSer(String key){
		return redisTemplate.opsForValue().get(key);
	}

	public void insert(String key, String value, long timeout, TimeUnit unit) {
		stringRedisTemplate.opsForValue().set(key, value, timeout, unit);
	}
	
}
